package com.marktotop.controller.business;


import com.baomidou.mybatisplus.extension.api.R;
import com.marktotop.entity.business.Vaccine;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.ISupplierService;
import com.marktotop.service.IVaccineService;
import com.marktotop.vo.business.VaccineSupplierVO;
import com.marktotop.vo.business.VaccineVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器 - 疫苗管理
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Api(tags = "业务模块-疫苗相关接口")
@RestController
@RequestMapping("/business/vaccine")
public class VaccineController {

    @Autowired
    private IVaccineService vaccineService;

    @Autowired
    private ISupplierService supplierService;


    /**
     * 分页展示所有疫苗列表，并进行模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param vaccineVO
     * @return
     */
    @ApiOperation(value = "疫苗列表", notes = "疫苗列表,根据疫苗名称模糊查询")
    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    public ResultBean findVaccinesList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       VaccineVO vaccineVO ){
        return vaccineService.findVaccinesList(pageNum,pageSize,vaccineVO);
    }


    /**
     * 获取所有疫苗供应商列表，供疫苗登记使用
     *
     * @return
     */
    @ApiOperation(value = "疫苗供应商列表", notes = "获取所有疫苗供应商列表，供疫苗登记使用")
    @RequestMapping(value = "/findVaccSupplierList",method = RequestMethod.GET)
    public ResultBean findVaccineSupplierList(){
        List<VaccineSupplierVO> vaccineSupplierList = supplierService.findVaccineSupplierList();
        if (vaccineSupplierList.isEmpty()){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("VaccSupplier",vaccineSupplierList);
    }


    /**
     * 获取某一疫苗完整信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取某一疫苗信息", notes = "获取某一类疫苗完整信息")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public ResultBean findVaccineInfo(@PathVariable Long id){
        VaccineVO vaccineInfo = vaccineService.getVaccineInfo(id);
        if (null == vaccineInfo){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("VaccInfo",vaccineInfo);
    }


    /**
     * 添加疫苗信息
     *
     * @param vaccine
     * @return
     */
    @ApiOperation(value = "添加疫苗信息", notes = "添加新的疫苗信息")
    @RequiresPermissions({"vaccine:add"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean addVaccines(@RequestBody @Validated Vaccine vaccine){
        vaccine.setCreateTime(new Date());
        vaccine.setModifiedTime(new Date());
        // 添加新疫苗时将剩余数设置为采购数量
        vaccine.setOverage(vaccine.getStock());
        boolean result = vaccineService.save(vaccine);
        if(!result){
            return ResultBean.error().message("添加疫苗信息失败！");
        }
        return ResultBean.ok().message("添加疫苗信息成功！");
    }


    /**
     * 编辑疫苗信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑疫苗信息", notes = "返回要编辑的疫苗信息对象")
    @RequiresPermissions({"vaccine:edit"})
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public ResultBean editVaccines(@PathVariable Long id){
        Vaccine vaccine = vaccineService.getById(id);
        if(null == vaccine ){
            return ResultBean.error().message("获取要修改的疫苗信息失败！");
        }
        return ResultBean.ok().data("vaccine",vaccine).message("获取修改的疫苗信息成功！");
    }


    /**
     * 更新疫苗信息
     *
     * @param vaccine
     * @return
     */
    @ApiOperation(value = "更新疫苗信息", notes = "更新疫苗信息记录")
    @RequiresPermissions({"vaccine:update"})
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    public ResultBean updateVaccines(@RequestBody @Validated Vaccine vaccine){
        vaccine.setModifiedTime(new Date());
        boolean result = vaccineService.updateById(vaccine);
        if(!result){
            return ResultBean.error().message("更新疫苗信息失败！");
        }
        return ResultBean.ok().message("更新疫苗信息成功！");
    }


    /**
     * 删除疫苗信息（删除剩余量为0的单个疫苗记录）
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除疫苗信息", notes = "删除某条疫苗，剩余量为0的记录")
    @RequiresPermissions({"vaccine:delete"})
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean deleteVaccines(@PathVariable Long id) throws SystemException {
        boolean result = vaccineService.removeVaccineRecord(id);
        if(!result){
            return ResultBean.error().message("删除疫苗信息失败！");
        }
        return ResultBean.ok().message("删除疫苗信息成功！");
    }
}
